//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __SDCLIENT_H_
#define __SDCLIENT_H_

#include "SDBusDriver.h"

#define SDMMC_INIT              200
#define SDMMC_SET_MULT_READ     208
#define SDMMC_SET_MULT_WRITE    209
#define SDMMC_SET_SINGLE_READ   210
#define SDMMC_SET_SINGLE_WRITE  211
#define SDMMC_GET_CAPACITY      212
#define SDMMC_GET_BLK_SIZE      213
#define SDMMC_GET_ERASE_SIZE    214

class CSDClient : public CDeviceDriver
{
    public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ Int32 * puNumberOfBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose();


public:
    CSDClient();
    ~CSDClient();


    void SDCarfPowerOff();
    void SDCardStartClock(void);
    void SDCardStopClock(void);
    void SDSetClockRate(XLLP_MMC_CLKRATE Rate, Boolean ClkOn);

    SD_API_STATUS Initialize(void);
    SD_API_STATUS StartIdentifyMode();
    SD_API_STATUS GetCardStatus();
    SD_API_STATUS InfoQueryBlockCaps();
    SD_API_STATUS InfoQuerySCR(PSD_COMMAND_RESPONSE);
    SD_API_STATUS InfoQueryCID(PSD_COMMAND_RESPONSE);
    SD_API_STATUS InfoQueryCSD(PSD_COMMAND_RESPONSE);
    SD_API_STATUS InfoQueryHostInterface();
    SD_API_STATUS InfoQueryRCA(PSD_COMMAND_RESPONSE);
    SD_API_STATUS InfoQueryStatus(PSD_COMMAND_RESPONSE);
    SD_API_STATUS SelectCurrentCard();
    SD_API_STATUS DeSelectCurCard();
    SD_API_STATUS SelectCardInterface(PSD_COMMAND_RESPONSE);
    SD_API_STATUS SetCardBlockLength(PSD_COMMAND_RESPONSE);
    SD_API_STATUS GetCardRegisters();
    SD_API_STATUS SDCardBlockRead(UInt32 addr, UInt32 nBlks, UInt8 *pBuffer);
    SD_API_STATUS SDCardBlockWrite(UInt32 addr, UInt32 nBlks, UInt8 *pBuffer);
    SD_API_STATUS SendSDAppCmd( XLLP_MMC_CMD         Command,
                                           UInt32                Argument,
                                           PSD_COMMAND_RESPONSE pResponse,
                                           UInt32                NumberOfBlocks,
                                           UInt32                BlockSize,
                                           PUInt8               pBlockBuffer);

    SD_API_STATUS SetCardPower();
    SD_API_STATUS DelayForPowerUp();

public:
    SD_CARD_STATE           m_CurState;
    SD_COMMAND_RESPONSE     m_ComResp;
    UInt32                   RefCnt;
    UInt16                  SD_RCA;
    Boolean                    m_4Dbus;
    SD_PARSED_REGISTER_CID  m_CID;
    SD_PARSED_REGISTER_CSD  m_CSD;
    SD_PARSED_REGISTER_SCR  m_SCR;
    SD_CARD_INFO            m_CardInfo;

};


#endif


